package com.bocai.kelala.ui.travel.set.presenter;

import android.util.Log;

import com.bocai.kelala.bean.CodeBean;
import com.bocai.kelala.ui.travel.main.contract.MainContract;
import com.bocai.kelala.ui.travel.set.contract.PwSetContract;
import com.bocai.kelala.util.AESUtils;
import com.bocai.kelala.util.RetryWithDelay;
import com.bocai.mybase.base.BaseResponse;
import com.bocai.mybase.net.Api;
import com.google.gson.Gson;

import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by yuanfei on 2017/6/20.
 */

public class PwSetPresenter extends PwSetContract.presenter {

    private final PwSetContract.view view;
    private final Api api;
    public String start = "{\"content\":";
    public String end = "}";
    Subscription subscription;

    @Inject
    public PwSetPresenter(PwSetContract.view view, Api api) {
        this.view = view;
        this.api = api;
    }

    @Override
    public void sendCode(String PhoneNumber, String RequestType) {
        view.showLoading("");
        api.sendCode(PhoneNumber,RequestType).subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    String result = AESUtils.decode(baseResponse.getData());
                    Log.e("result",result);
                    CodeBean bean = new Gson().fromJson(result,CodeBean.class);
                    view.getCode(bean.getCode());
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }

    @Override
    public void resetPw(String phone, String pw, String code) {
        view.showLoading("");
        api.resetPw(phone,pw,code,"1").subscribeOn(Schedulers.io()).retryWhen(new RetryWithDelay(3,3000))
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<BaseResponse>() {
            @Override
            public void onCompleted() {
                view.stopLoading();
            }

            @Override
            public void onError(Throwable e) {
                Log.e("Throwable",e.toString());
                view.stopLoading();
                view.showErrorTip("网络异常");
            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                view.stopLoading();
                Log.e("baseResponse",baseResponse.toString());
                if (baseResponse.getReturnCode().equals("200")){
                    view.resetPw();
                }else{
                    view.showErrorTip(baseResponse.getMsg());
                }
            }
        });
    }
    @Override
    public void setCode() {
        subscription = Observable.interval(1, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).
                observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Long aLong) {
                view.setBtnCode((int) (59 - aLong));
                if (aLong == 59) {
                    subscription.unsubscribe();
                }
            }
        });
    }
}
